package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Brand;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/6/10.
 */
public class BrandListAdapter extends BaseAdapter {
    private ArrayList<Brand> mBrands = new ArrayList<>();
    private ArrayList<Brand> mBrandsTemps = new ArrayList<>();


    public BrandListAdapter(ArrayList<Brand> brands){
        this.mBrands=brands;
    }

    public void setmBrandsTemps(ArrayList<Brand> brands){
        /*if (brands != null)
        {

            if (mBrandsTemps.size() <= 0)
            {
                mBrandsTemps.addAll(brands);
            }
            else
            {
                for (Brand brand:brands)
                {
                    boolean bContain = false;
                    for (Brand brand1 : mBrandsTemps)
                    {
                        if (brand.id == brand1.id)
                        {
                            bContain = true;
                            Log.e("","");
                            break;
                        }
                    }
                    if (!bContain)
                        mBrandsTemps.add(brand);
                }
            }
        }*/
        mBrandsTemps = mBrands;
    }

    @Override
    public int getCount() {
        return mBrands == null ? 0 : mBrands.size();

    }

    @Override
    public Brand getItem(int position) {
        return mBrands.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.branch_list_item, parent, false);
            holder.mBrandTitle = (TextView) convertView.findViewById(R.id.list_item_des);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Brand item = getItem(position);

        if (item != null)
        {
            holder.mBrandTitle.setText(item.title);
        }
        return convertView;
    }


    public static class ViewHolder{
        public TextView mBrandTitle;
    }

    public void filter(String charText) {
        if (charText != null)
        {
            mBrands.clear();
            if (charText.length() == 0) {
                mBrands.addAll(mBrandsTemps);
            } else {
                for (Brand brand : mBrandsTemps) {
                    if (brand.title.contains(charText)) {
                        mBrands.add(brand);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }
}
